﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Net5ApiAndEfMySql.DtoModel;
using Net5ApiAndEfMySql.DtoModel.Common;
using Net5CommonLib.SystemLib.Tools.FileTools;


namespace Net5ApiAndEfMySql.API.Controllers
{
    public class FileManagerController : BaseApiController
    {
        private IConfiguration _iConfiguration;
        private string configPath = "";

        public FileManagerController(IConfiguration iConfiguration)
        {
            _iConfiguration = iConfiguration;
            configPath = _iConfiguration.GetSection("MyConfigAppSeting")["FileUploadPath"];
        }

        [HttpPost]
        public async Task<ResultModel> FileUpload(List<IFormFile> fileList)
        {
            ResultModel result = new ResultModel();
            try
            {
                List<FileUpLoadResult> fulList = new List<FileUpLoadResult>();
                if (fileList != null && fileList.Count > 0)
                {
                    foreach (var i in fileList)
                    {
                        FileUpLoadResult ful = new FileUpLoadResult();
                        string fileName = Guid.NewGuid() + Path.GetExtension(i.FileName);
                        string urlPath = configPath + "/" + fileName;
                        string upLoadPath = FileTools.MapPath("/wwwroot" + configPath);
                        ful.FileOldName = i.FileName;
                        ful.FileNewName = fileName;
                        ful.FileNewPath = urlPath;
                        if (!Directory.Exists(upLoadPath))
                        {
                            Directory.CreateDirectory(upLoadPath);
                        }

                        try
                        {
                            using (var fs = new FileStream(upLoadPath+"/"+fileName,FileMode.Create))
                            {
                                await i.CopyToAsync(fs);
                                fs.Flush();
                                ful.IsUpload = true;

                            }
                        }
                        catch (Exception e)
                        {
                            ful.IsUpload = false;
                            throw;
                        }
                        fulList.Add(ful);

                    }

                    result.code = ResultCode.Ok;
                    result.data = fulList;
                    result.mess = "成功";

                }
                else
                {
                    result.code = ResultCode.Ok;
                    result.data = null;
                    result.mess = "失败!";
                }

            }
            catch (Exception e)
            {
                result.code = ResultCode.Error;
                result.mess = "错误!";
            }

            return result;
        }

        public ResultModel DeleteImage(string imageFileName)
        {
            ResultModel result = new ResultModel();

            try
            {
                if (!string.IsNullOrWhiteSpace(imageFileName))
                {
                    string filePath = "/wwwroot" + configPath + "/" + imageFileName;
                    string dir = FileTools.MapPath(filePath);
                    System.IO.File.Delete(dir);
                    result.code = ResultCode.Ok;
                }
                else
                {
                    result.code = ResultCode.Fail;
                }
            }
            catch (Exception e)
            {

                result.code = ResultCode.Error;
                result.mess = "错误";

            }
            return result;
        }
    }
}
